#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/3 上午10:47
# @Author  : nabby
# @File    : role_api.py
# @Desc:

from app import app, request
import json
from application.role_manage import RoleService
from infrastructure.utils.http import responser
from infrastructure.utils.decorator.oauth2_tool import oauth2_check


@app.route('/api/get_role_list', endpoint='get_role_list', methods=["POST"])
@oauth2_check
def get_role_list():
    res_status, rjson = responser.post_param_check(request, [
        'name',
        'page',
        'pageSize'
    ])
    if res_status == 'success':
        return RoleService.get_role_list(
            name=rjson.get('name'),
            page=int(rjson.get('page', 1)),
            page_size=int(rjson.get('pageSize', 999))
        )
    else:
        return rjson


@app.route('/api/add_role', endpoint='add_role', methods=['POST'])
@oauth2_check
def add_role():
    res_status, rjson = responser.post_param_check(request, ['name'])
    if res_status == 'success':
        name = rjson.get("name")
        description = rjson.get("description")
        return RoleService.add_role(name=name, description=description)
    else:
        return rjson


@app.route('/api/update_role', endpoint='update_role', methods=['POST'])
@oauth2_check
def update_role():
    res_status, rjson = responser.post_param_check(request, [
        'roleId',
        'name'
    ])
    if res_status == 'success':
        role_id = rjson.get("roleId")
        name = rjson.get("name")
        description = rjson.get("description")
        return RoleService.update_role(role_id=role_id, name=name, description=description)
    else:
        return rjson


@app.route('/api/change_role_state', endpoint='change_role_state', methods=['POST'])
@oauth2_check
def change_role_state():
    res_status, rjson = responser.post_param_check(request, [
        'roleId',
        'status'
    ])
    if res_status == 'success':
        return RoleService.change_role_state(
            role_id=rjson.get("roleId"),
            state=rjson.get("status")
        )
    else:
        return rjson


@app.route('/api/delete_role', endpoint='delete_role', methods=['POST'])
@oauth2_check
def delete_role():
    res_status, rjson = responser.post_param_check(request, [
        'roleId',
    ])
    if res_status == 'success':
        return RoleService.delete_role(
            role_id=rjson.get("roleId"),
        )
    else:
        return rjson

# @app.route('/api/get_menu_tree', endpoint='get_menu_tree', methods=['POST'])
# def get_menu_tree():
#     res_status, rjson = responser.post_param_check(request, [
#     ])
#     if res_status =='success':
#         return RoleService.get_menu_tree()
#     else:
#         return rjson

@app.route('/api/get_menu_tree', endpoint='get_menu_tree', methods=['POST'])
@oauth2_check
def get_menu_tree():
    res_status, rjson = responser.post_param_check(request, [
    ])
    if res_status =='success':
        return RoleService.get_menu_tree()
    else:
        return rjson

@app.route('/api/set_role_menu', endpoint='set_role_menu', methods=['POST'])
@oauth2_check
def set_role_menu():
    res_status, rjson = responser.post_param_check(request, [
        'roleId',
        'rolePermission'
    ])
    if res_status =='success':
        return RoleService.set_role_menu(
            role_id=rjson.get("roleId"),
            menu_tree=json.loads(rjson.get("rolePermission"))
        )
    else:
        return rjson
